<?php
// +----------------------------------------------------------------------
// | ManniuRead
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://manniuyun.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: heitao <mall@manniuyun.com>
// +----------------------------------------------------------------------

namespace app\mall\controller\admin;

use app\mall\base\MallAdminBase;

use app\mall\model\MallPayment as MallPaymentModel;

class Payment extends MallAdminBase{
    //当前控制器菜单
    public $submenu = [];

    /**
     * TP初始化方法
     */
    public function _initialize() {
        parent::_initialize();
        $this->submenu[] = ['title' => '支付方式列表', 'url' => url('mall/admin.payment/index'), 'icon' => '&#xe60a;'];
    }

    /**
     * 商城支付方式列表
     * @access public
     * @return \think\Response
     */
    public function index(){
        $list = MallPaymentModel::order('pay_order asc,id desc')->paginate(config('mn_admin.admin_pagesize'));
        $this->assign([
            'submenu' => $this->submenu,
            'list' => $list
        ]);
        return $this->fetch();
    }

    /**
     * 商城支付方式增加
     * @access public
     * @return \think\Response
     */
    public function add(){
        // POST提交数据
        if($this->request->isPost()){
            // 获取提交数据
            $post_data = $this->buildParam();
            // 实例化支付方式模型
            $MallPaymentModel = new MallPaymentModel;
            // 验证支付方式名称是否重复
            $check_repeat = MallPaymentModel::where('pay_name',$post_data['pay_name'])->count();
            if($check_repeat){
                $this->error('支付方式名称重复');
            }
            // 新增操作
            if($MallPaymentModel->allowField(true)->validate(true)->save($post_data)){
                // 更新所有规格缓存
                $MallPaymentModel->cache();
                $this->success('支付方式添加成功','index');
            }
            else{
                $this->error($MallPaymentModel->getError());
            }
        }
        $this->assign([
            'submenu' => $this->submenu
        ]);
        return $this->fetch();
    }

    /**
     * 商城支付方式修改
     * @access public
     * @return \think\Response
     */
    public function edit($id = 0){
        $id = (int)$id;
        if(!$id){
            $this->error('缺少参数id','index');
        }
        // 实例化栏目模型
        $MallPaymentModel = new MallPaymentModel;
        // 查询商品信息
        $pay = MallPaymentModel::get($id);
        if(!$pay){
            $this->error('支付方式不存在','index');
        }
        // POST提交数据
        if($this->request->isPost()){
            // 获取提交数据
            $post_data = $this->buildParam();
            // 实例化支付方式模型
            $MallPaymentModel = new MallPaymentModel;
            // 验证支付方式名称是否重复
            $check_repeat = MallPaymentModel::where('id','neq',$id)->where('pay_name',$post_data['pay_name'])->count();
            if($check_repeat){
                $this->error('支付方式名称重复');
            }
            // 新增操作
            if(false !== $MallPaymentModel->allowField(true)->validate(true)->save($post_data,['id'=>$id])){
                // 更新所有规格缓存
                $MallPaymentModel->cache();
                $this->success('支付方式修改成功','index');
            }
            else{
                $this->error($MallPaymentModel->getError());
            }
        }
        $this->assign([
            'submenu' => $this->submenu,
            'data' => $pay
        ]);
        return $this->fetch();
    }
}